package Three_ZhuChangShuang.menu;

import Three_ZhuChangShuang.entity.Adenovirus;
import Three_ZhuChangShuang.entity.Person;

import java.util.Scanner;

/**
 * @Author zhu
 * @Date 2021/3/24 11:34
 * @ClassNmae 主菜单
 * @Description TOMD
 */
public class Menu {

    AdminMenu adminMenu;
    UserMenu userMenu;

    private int count = 0;
    Person persons[];

    Scanner scanner = new Scanner(System.in);

    public AdminMenu getAdminMenu() {
        return adminMenu;
    }

    public void setAdminMenu(AdminMenu adminMenu) {
        this.adminMenu = adminMenu;
    }

    public UserMenu getUserMenu() {
        return userMenu;
    }

    public void setUserMenu(UserMenu userMenu) {
        this.userMenu = userMenu;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Person[] getPersons() {
        return persons;
    }

    public void setPersons(Person[] persons) {
        this.persons = persons;
    }

    /**
     * @return void
     * @Author zhu
     * @Description 开始主菜单
     * @Date 2021/3/23
     */
    public void start() {
        boolean isExit = false;
        while (!isExit) {
            System.out.println("***********欢迎使用新冠疫苗接种系统***********");
            System.out.println("请选择：\t 1.用户注册 \t 2.用户登录 \t 0.退出系统");
            System.out.println("******************************************");
            try {
                int choose = Integer.parseInt(scanner.next());
                switch (choose) {
                    case 1: {
                        register();
                        break;
                    }
                    case 2: {
                        login();
                        break;
                    }
                    case 0: {
                        System.out.println("退出系统");
                        return;
                    }
                    default: {
                        System.out.println("输入不合法，请输入正确的选项！");
                    }
                }
            } catch (Exception e) {
                //e.printStackTrace();
                System.out.println("输入不合法，请输入正确的选项！");
            }
        }
    }

    /**
     * @return void
     * @Author zhu
     * @Description 用户注册
     * @Date 2021/3/24 15:02
     */
    public void register() {
        for (int i = 0; i < persons.length; i++) {
            if (persons[i] == null) {
                Person person = new Person();
                System.out.println("请输入用户名：");
                String name = scanner.next();
                boolean check = check(name, persons);
                //判断该用户是否存在
                if (!check) {
                    person.setName(name);
                    System.out.println("请输入密码：");
                    person.setPwd(scanner.next());
                    System.out.println("请输入联系电话：");
                    person.setNumber(scanner.next());
                    persons[i] = person;
                    System.out.println("注册成功");
                    return;
                } else System.out.println("该用户名已经存在！");
            }
        }
    }

    /**
     * @return boolean
     * @Author zhu
     * @Description 检查是否已经存在传入的用户名（ture代表已经存在该用户名）
     * @Date 2021/3/25 18:31
     */
    private boolean check(String name, Person[] persons) {
        boolean flag = false;
        for (int i = 0; i < persons.length; i++) {
            if (persons[i] != null && persons[i].getName().equals(name)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * @return void
     * @Author zhu
     * @Description 用户登录
     * @Date 2021/3/24 15:02
     */
    public void login() {
        String account;
        String password;
        System.out.println("请输入登录账号：");
        account = scanner.next();

        System.out.println("请输入账号密码：");
        password = scanner.next();
        for (int i = 0; i < persons.length; i++) {
            if (persons[i] != null) {
                //进入管理员页面
                if (account.equals("admin") && password.equals("admin")) {
                    System.out.println("管理员登录成功！");
                    adminMenu.adminView();
                    return;
                } else if (account.equals(persons[i].getName()) && password.equals(persons[i].getPwd())) {
                    setCount(i);
                    System.out.println("登录成功！");
                    userMenu.userView();
                    return;
                }
            }
        }
        System.out.println("账号或者密码错误！");
        return;
    }
}
